#define _UART_C_
/* Includes ---------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "fm33lg0xx.h"
#include "uart.h"
/* USER CODE END Includes */

/* Types ------------------------------------------------------------*/
/* USER CODE BEGIN Types */

/* USER CODE END Types */

/* Constants --------------------------------------------------------*/
/* USER CODE BEGIN Constants */

/* USER CODE END Constants */

/* Macro ------------------------------------------------------------*/
/* USER CODE BEGIN Macro */

/* USER CODE END Macro */

/* Variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
STUartData stUart4Rx;
/* USER CODE END Variables */

/* Function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FP */
static void vUART0GPIOInit(void);
static void vUART0InterruptInit(void);
static void vUART0NVICInit(void);

static void vUART4GPIOInit(void);
static void vUART4InterruptInit(void);
static void vUART4NVICInit(void);
/* USER CODE END FP */

void vUART4Init(void)
{
	vUART4GPIOInit();
	vUART4InterruptInit();
	vUART4NVICInit();
}

void vUART0Init(void)
{
	vUART0GPIOInit();
	vUART0InterruptInit();
	vUART0NVICInit();
}

static void vUART4GPIOInit(void)
{
    FL_GPIO_InitTypeDef GPIO_InitStruct;

    FL_UART_InitTypeDef UART4_InitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_0;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_ENABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.pin = FL_GPIO_PIN_1;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    UART4_InitStruct.clockSrc = FL_CMU_UART0_CLK_SOURCE_APBCLK;
    UART4_InitStruct.baudRate = 9600;
    UART4_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;
    UART4_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
    UART4_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
    UART4_InitStruct.parity = FL_UART_PARITY_NONE;

    FL_UART_Init(UART4, &UART4_InitStruct);
}

static void vUART0GPIOInit(void)
{
    FL_GPIO_InitTypeDef GPIO_InitStruct;

    FL_UART_InitTypeDef UART0_InitStruct;

    GPIO_InitStruct.pin = FL_GPIO_PIN_2;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_ENABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.pin = FL_GPIO_PIN_3;
    GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
    GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
    GPIO_InitStruct.pull = FL_DISABLE;
    GPIO_InitStruct.remapPin = FL_DISABLE;
    GPIO_InitStruct.analogSwitch = FL_DISABLE;
    FL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    UART0_InitStruct.clockSrc = FL_CMU_UART0_CLK_SOURCE_APBCLK;
    UART0_InitStruct.baudRate = 1200;
    UART0_InitStruct.transferDirection = FL_UART_DIRECTION_TX_RX;
    UART0_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B;
    UART0_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B;
    UART0_InitStruct.parity = FL_UART_PARITY_ODD;

    FL_UART_Init(UART0, &UART0_InitStruct);
}

static void vUART4InterruptInit(void)
{
	FL_UART_ClearFlag_RXBuffFull(UART4);
    FL_UART_EnableIT_RXBuffFull(UART4);
}

static void vUART0InterruptInit(void)
{
	FL_UART_ClearFlag_RXBuffFull(UART0);
    FL_UART_EnableIT_RXBuffFull(UART0);
}

static void vUART4NVICInit(void)
{
	FL_NVIC_ConfigTypeDef InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x03;
    FL_NVIC_Init(&InterruptConfigStruct, UART4_IRQn);
}

static void vUART0NVICInit(void)
{
	FL_NVIC_ConfigTypeDef InterruptConfigStruct;

    InterruptConfigStruct.preemptPriority = 0x02;
    FL_NVIC_Init(&InterruptConfigStruct, UART0_IRQn);
}

void vUart4TxData(uint8_t *pdata, uint8_t ucSize)
{
	uint8_t i = 0;
	
	for(i = 0; i < ucSize; i++)
	{
		FL_UART_WriteTXBuff(UART4, (uint8_t)pdata[i]);
		while(FL_UART_IsActiveFlag_TXBuffEmpty(UART4) != FL_SET);
    }
}

void vUart4RxBufferClr(void)
{
	memset((uint8_t *)stUart4Rx.ucAryBuffer, 0x00, UART_RX_BUFFER_SIZE);
	stUart4Rx.ucSize = 0;
}

void UART4_IRQHandler(void)
{
    uint32_t ulUartRxBuffFullIT = 0;
    uint32_t ulUartRxBuffFullFlg = 0;

    ulUartRxBuffFullIT = FL_UART_IsEnabledIT_RXBuffFull(UART4);
    ulUartRxBuffFullFlg = FL_UART_IsActiveFlag_RXBuffFull(UART4);

    if((ulUartRxBuffFullIT == 0x01UL) && (ulUartRxBuffFullFlg == 0x01UL))
    {
		stUart4Rx.ucAryBuffer[stUart4Rx.ucSize++] = FL_UART_ReadRXBuff(UART4);
		gucUart4RxTimer = 0;
		SET_UART4_RX_START_FLG();
    }
}

void UART0_IRQHandler(void)
{
    uint32_t ulUartRxBuffFullIT = 0;
    uint32_t ulUartRxBuffFullFlg = 0;

    ulUartRxBuffFullIT = FL_UART_IsEnabledIT_RXBuffFull(UART0);
    ulUartRxBuffFullFlg = FL_UART_IsActiveFlag_RXBuffFull(UART0);

    if((ulUartRxBuffFullIT == 0x01UL) && (ulUartRxBuffFullFlg == 0x01UL))
    {
		stHartRxData.ucAryBuffer[stHartRxData.uiDataCnt++] = FL_UART_ReadRXBuff(UART0);
		gucUart0RxTimer = 0;
		SET_UART0_HART_RX_START_FLG();
    }
}

int fputc(int ch, FILE *f)
{
    FL_UART_WriteTXBuff(UART4, (uint8_t)ch);
    while(FL_UART_IsActiveFlag_TXBuffEmpty(UART4) != FL_SET);
    return ch;
}
